#!/bin/bash

## Redis Env
yum -y install gcc libc

## upgrade gcc
#yum -y install centos-release-scl
#yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#scl enable devtoolset-9 bash
#yum -y install tcl


# Directory of tar.gz
redis_source=$1
# Redis installation directory
redis_prefix=$2


if [ ! -d "${redis_source}" ]; then
        echo ">>Failed: redis_source directory is not exist, create it and execute. "
        exit 1
fi

if [ ! -d "${redis_prefix}" ]; then
        echo ">>Failed: directory of installing redis is not exist, create it and execute."
        exit 1
fi

echo ">>Continue: Start downloading redis. "

redis_url="https://download.redis.io/releases/redis-5.0.13.tar.gz"

cd ${redis_source} && wget ${redis_url}

if [ $? -eq "0" ]; then
        echo ">>Succeed: download redis successfully ..."
else
        echo ">>Failed: redis download failed, installation interrupted "
        exit 4
fi

echo ">>Continue: Start extracting to the installation directory "

redis_name=$(echo ${redis_url} | awk -F '/' '{print $NF}')
tar_content=$(tar -xvf ${redis_name} )
redis_home=$(echo $tar_content | cut -d " " -f 1)

echo ">>Succeed: Decompression complete. "

echo ">>Continue: Start compilation. "

cd ${redis_home} && make && make PREFIX=${redis_prefix}/redis install

if [ $? -eq "0" ];then
  echo ">>Succeed: Configuration complete. "
else
  echo ">>Failed: Configuration failed. please check"
  exit 2
fi

echo ">>Continue: Add environment variable. "
cat >>/etc/profile <<EOF

## Redis Env
export REDIS_HOME=${redis_prefix}/redis
EOF

cat >>/etc/profile <<'EOF'
export PATH=${REDIS_HOME}/bin:$PATH

EOF

## change env
. /etc/profile

redis-server --version

if [ $? -eq "0" ];then
        echo ">>Succeed: Redis env successfully. "
else
        echo ">>Failed: Redis env failed .please check. "
fi


echo ">>Continue: redis.conf is viming..."

cp redis.conf redis.conf.bak
## vim redis.conf
line=$(grep -n "^bind 127.0.0.1" redis.conf|awk -F ':' '{print $1}')
sed -ie "${line}c bind 0.0.0.0" redis.conf

line=$(grep -n "^daemonize no$" redis.conf|awk -F ':' '{print $1}')
sed -ie "${line}c daemonize yes" redis.conf

line=$(grep -n "^protected-mode yes$" redis.conf|awk -F ':' '{print $1}')
sed -ie "${line}c protected-mode no" redis.conf

line=$(grep -n "^pidfile.*pid$" redis.conf|awk -F ':' '{print $1}')
sed -ie "${line}c pidfile ${redis_prefix}/redis/6379.pid" redis.conf


line=$(grep -n "^logfile" redis.conf|awk -F ':' '{print $1}')
sed -ie "${line}c logfile ${redis_prefix}/redis/6379.log" redis.conf
touch ${redis_prefix}/redis/6379.log

line=$(grep -n "^dir ./$" redis.conf|awk -F ':' '{print $1}')
sed -ie "${line}c dir ${redis_prefix}/redis/working" redis.conf
mkdir ${redis_prefix}/redis/working

line=$(grep -n "requirepass foobared$" redis.conf|awk -F ':' '{print $1}')
sed -ie "${line}c requirepass 123456" redis.conf

cp redis.conf ${redis_prefix}/redis/

echo ">>Succeed: redis.conf is finished."

echo ">>Continue: redis.sh is viming..."
cp utils/redis_init_script redis.sh

line=$(grep -n "^EXEC=" redis.sh|awk -F ':' '{print $1}')
sed -ie "${line}c EXEC=${redis_prefix}/redis/bin/redis-server" redis.sh

line=$(grep -n "^CLIEXEC=" redis.sh|awk -F ':' '{print $1}')
sed -ie "${line}c CLIEXEC=${redis_prefix}/redis/bin/redis-cli" redis.sh

line=$(grep -n "^PIDFILE=" redis.sh|awk -F ':' '{print $1}')
sed -ie "${line}c PIDFILE=${redis_prefix}/redis/6379.pid" redis.sh

line=$(grep -n "^CONF=" redis.sh|awk -F ':' '{print $1}')
sed -ie "${line}c CONF=${redis_prefix}/redis/redis.conf" redis.sh

line=$(grep -n "^\s*\$CLIEXEC" redis.sh|awk -F ':' '{print $1}')
sed -ie "${line}c \$CLIEXEC -a 123456 -p \$REDISPORT shutdown" redis.sh

mv redis.sh ${redis_prefix}/redis
echo ">>Succeed: redis.sh is finished."


